//
//  ShipViewController.m
//  VTravel
//
//  Created by lanouhn on 16/8/5.
//  Copyright © 2016年 xiaoYu. All rights reserved.
//

#import "ShipViewController.h"
/** 引入自定义cell */
#import "ShipListTableViewCell.h"

/** 引入轮播图model */
#import "RootScrollViewModel.h"
/** 航线model */
#import "ShipLineModel.h"
/** 邮轮日历model */
#import "CalenderModel.h"
/** 列表model */
#import "ShipProductListModel.h"

#import "RootTableHeaderView.h"

@interface ShipViewController ()<UITableViewDelegate, UITableViewDataSource, RootTableHeaderViewDelegate>

/** 底部表视图 */
@property (strong, nonatomic) IBOutlet UITableView *rootTableView;

/** 轮播图对象 用来接收表头的轮播图属性,从而在请求数据成功之后给轮播图设置数据源 */
@property (nonatomic, strong) SDCycleScrollView *sdcScrollView;

/** 存放轮播图实体的数据源数组 */
@property (nonatomic, strong) NSMutableArray *ShipSVDataArray;
/** 存放轮播图图片的数据源数组 */
@property (nonatomic, strong) NSMutableArray *ShipSVImageArray;
/** 航线btn图片数组 */
@property (nonatomic, strong) NSMutableArray *ShipLineImageArray;
/** 航线btn标题数组 */
@property (nonatomic, strong) NSMutableArray *ShipLineTitleArray;
/** 航线产品列表数组 */
@property (nonatomic, strong) NSMutableArray *ShipProductListArray;
/** 日历数据源数组 */
@property (nonatomic, strong) NSMutableArray *CalenderArray;

@end

@implementation ShipViewController

#pragma mark - 懒加载
- (void)lazyLoad{
    self.ShipSVDataArray = [NSMutableArray array];
    self.ShipSVImageArray = [NSMutableArray array];
    self.ShipLineImageArray = [NSMutableArray array];
    self.ShipLineTitleArray = [NSMutableArray array];
    self.ShipProductListArray = [NSMutableArray array];
    self.CalenderArray = [NSMutableArray array];
}

#pragma mark - 请求邮轮首页数据的方法
- (void)requestData{
    [NetWorkManager requestForGETWithURL:KShipRootURL paramater:@{@"assistant_id":@"0",@"city_id":city_id} success:^(id responseObject) {
        
        // 封装轮播图数据
        for (NSDictionary *dic in responseObject[@"data"][@"ad"]) {
            // 轮播图的数据结构与主页的RootScrollViewModel数据结构一样,可以使用主页的RootScrollViewModel
            RootScrollViewModel *model = [[RootScrollViewModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            model.src = ImageURL(dic[@"src"]);
            [self.ShipSVDataArray addObject:model];
            [self.ShipSVImageArray addObject:model.src];
        }
        
        // 封装航线btn
        for (NSDictionary *dic in responseObject[@"data"][@"line_list"]) {
            ShipLineModel *model = [[ShipLineModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ShipLineTitleArray addObject:model.line_name];
            [self.ShipLineImageArray addObject:ImageURL(model.pic)];
        }

        // 日历数据
        for (NSDictionary *dic in responseObject[@"data"][@"rili"]) {
            CalenderModel *model = [[CalenderModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
        }

        // 封装航线产品列表
        for (NSDictionary *dic in responseObject[@"data"][@"tehuichanpin"]) {
            ShipProductListModel *model = [[ShipProductListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ShipProductListArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            // 创建表头
            [self creatTableHeaderView];
            // 设置轮播图展示的数据源
            self.sdcScrollView.imageURLStringsGroup = self.ShipSVImageArray;
            [self.rootTableView reloadData];
        });
        
    } fail:^(NSError *error) {
        kMyLog(@"%@", error);
    }];
}

#pragma mark - 视图加载完成的方法
- (void)viewDidLoad {
    [super viewDidLoad];

    // 懒加载初始化数据源
    [self lazyLoad];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 请求数据
        [self requestData];
    });
    
    // 创建表头视图
    [self creatTableHeaderView];

    // 设置cell的自适应高度
    self.rootTableView.estimatedRowHeight = 200;
    self.rootTableView.rowHeight = UITableViewAutomaticDimension;
    
    // 注册cell
    [self.rootTableView registerNib:[UINib nibWithNibName:@"ShipListTableViewCell" bundle:nil] forCellReuseIdentifier:@"ShipListTableViewCell"];
}
#pragma mark - 表头视图
- (void)creatTableHeaderView{
    RootTableHeaderView *headerView = [[RootTableHeaderView alloc] initWithShipHVwithFram:CGRectMake(0, 0, kScreenWidth, 320) btnTitleArr:self.ShipLineTitleArray btnImageUrlArr:self.ShipLineImageArray];
    // 接收轮播图对象
    self.sdcScrollView = headerView.sdcycleScrollView;
    // 设置表头代理
    headerView.delegate = self;
    // 点击自定义button的回调
    headerView.customBtnBlock = ^(NSInteger index){
        kMyLog(@"点击了%@",self.ShipLineTitleArray[index]);
    };
    self.rootTableView.tableHeaderView = headerView;
}
#pragma mark - 点击轮播图图片的代理方法
- (void)clickScrollView:(SDCycleScrollView *)scrollView andIVIndex:(NSInteger)index{
    kMyLog(@"点击了%ld张图片", index);
}

#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
#pragma mark - 每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return self.ShipProductListArray.count + 1;
    }
}
#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 重用
    static NSString *identifier = @"CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell  == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    // cell的选中样式
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        cell.imageView.image = [UIImage imageNamed:@"邮轮日历"];
        cell.textLabel.text = @"邮轮日历";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"精品航线"];
            cell.textLabel.text = @"精品航线";
        }else{
            
            ShipListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShipListTableViewCell" forIndexPath:indexPath];
            if (self.ShipProductListArray.count != 0) {
                cell.model = self.ShipProductListArray[indexPath.row - 1];
            }
            return cell;
        }
    }
    return cell;
}
#pragma mark - 分区区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
#pragma mark - 返回行高的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (indexPath.section == 1 && indexPath.row != 0) ? -1 : 44;
}
#pragma mark - cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


@end
